package com.locationtracker.plus.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.locationtracker.plus.app.network.ListDataUiState
import com.locationtracker.plus.app.network.apiService
import com.locationtracker.plus.app.util.CacheUtil
import com.locationtracker.plus.data.model.bean.MemberResponse
import com.locationtracker.plus.data.model.bean.RouteLocation
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.requestNoCheck

class RequestMemberViewModel : BaseViewModel() {

    private var pageNo = 1

    var memberDataState: MutableLiveData<ListDataUiState<MemberResponse>> = MutableLiveData()

    fun getMemberData(isRefresh: Boolean) {
        if (isRefresh) {
            pageNo = 1
        }
        requestNoCheck({
            apiService.friendList(pageNo)
        }, { response ->
            pageNo++
            if (isRefresh) {
                val location = CacheUtil.getLastLocation()
                response.data.add(
                    0,
                    MemberResponse("", "Me", location?.address ?: "", location?.date ?: "")
                )
            }
            val listDataUiState =
                ListDataUiState(
                    isSuccess = response.isSucces(),
                    isRefresh = isRefresh,
                    isEmpty = response.isSucces() && response.data.isEmpty(),
                    hasMore = pageNo <= response.meta.pagination.total_pages,
                    isFirstEmpty = isRefresh && response.data.isEmpty(),
                    listData = response.data
                )
            memberDataState.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    isRefresh = isRefresh,
                    listData = arrayListOf<MemberResponse>()
                )
            memberDataState.value = listDataUiState
        })
    }

    private val gson by lazy { Gson() }
    var routeList = MutableLiveData<ArrayList<RouteLocation>>()

    fun myRoute(startTime: String, endTime: String) {
        requestNoCheck({
            apiService.myRoute(startTime, endTime)
        }, { response ->
            if (!response.isSucces()) {
                ToastUtils.showShort(response.getResponseMsg())
                return@requestNoCheck
            }
            val list = arrayListOf<RouteLocation>()
            if (response.data.has("positions")) {
                response.data["positions"].asJsonArray.forEach { element ->
                    list.add(gson.fromJson(element, RouteLocation::class.java))
                }
            }
            routeList.value = list
        }, {
            ToastUtils.showShort(it.errorMsg)
        }, isShowDialog = false)
    }

    fun friendRoute(friendId: String, startTime: String, endTime: String) {
        requestNoCheck({
            apiService.friendRoute(friendId, startTime, endTime)
        }, { response ->
            if (!response.isSucces()) {
                ToastUtils.showShort(response.getResponseMsg())
                return@requestNoCheck
            }
            val list = arrayListOf<RouteLocation>()
            if (response.data.has("positions")) {
                response.data["positions"].asJsonArray.forEach { element ->
                    list.add(gson.fromJson(element, RouteLocation::class.java))
                }
            }
            routeList.value = list
        }, {
            ToastUtils.showShort(it.errorMsg)
        }, isShowDialog = false)
    }
}